Skip to content

MkTable

:fa-brands-github: Show source on GitHub

Class representing a formatted table.

Description

Data: a mapping column -> cells, a list of rows (the first one is the header unless columns are given) or a list of dicts (see MkBaseTable). | in cells gets escaped.

Example: Regular

Jinja

{{ {"Column A": ["A", "B", "C"], "Column B": ["C", "D", "E"]} | MkTable }}

Python

MkTable(data={'Column A': ['A', 'B', 'C'], 'Column B': ['C', 'D', 'E']})
Column A Column B
A C
B D
C E
| Column A | Column B |
| -------- | -------- |
| A        | C        |
| B        | D        |
| C        | E        |
<table>
<thead>
<tr>
<th>Column A</th>
<th>Column B</th>
</tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td>C</td>
</tr>
<tr>
<td>B</td>
<td>D</td>
</tr>
<tr>
<td>C</td>
<td>E</td>
</tr>
</tbody>
</table>
MkTable
├── MkText('A')
├── MkText('B')
├── MkText('C')
├── MkText('C')
├── MkText('D')
╰── MkText('E')

Example: From rows (the first one is the header unless columns are given)

Jinja

{{ [["Name", "Value"], ["a|b", "1"], ["c", "2"]] | MkTable }}

Python

MkTable(data={'Name': ['a|b', 'c'], 'Value': ['1', '2']})
Name Value
a|b 1
c 2
| Name | Value |
| ---- | ----- |
| a\|b | 1     |
| c    | 2     |
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>a|b</td>
<td>1</td>
</tr>
<tr>
<td>c</td>
<td>2</td>
</tr>
</tbody>
</table>
MkTable
├── MkText('a|b')
├── MkText('c')
├── MkText('1')
╰── MkText('2')

Example: From list of dicts

Jinja

{{ [{"col 1": "abc", "col 2": "cde"}, {"col 1": "fgh", "col 2": "ijk"}] | MkTable }}

Python

MkTable(data={'col 1': ['abc', 'fgh'], 'col 2': ['cde', 'ijk']})
col 1 col 2
abc cde
fgh ijk
| col 1 | col 2 |
| ----- | ----- |
| abc   | cde   |
| fgh   | ijk   |
<table>
<thead>
<tr>
<th>col 1</th>
<th>col 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>abc</td>
<td>cde</td>
</tr>
<tr>
<td>fgh</td>
<td>ijk</td>
</tr>
</tbody>
</table>
MkTable
├── MkText('abc')
├── MkText('fgh')
├── MkText('cde')
╰── MkText('ijk')

Bases: MkBaseTable

Name Children Inherits
MkDependencyGroups
mknodes.templatenodes.mkdependencygroups
Node to display optional dependencies or dependency groups as a table.
    Name Children Inherits
    MkBaseTable
    mknodes.basenodes.mkbasetable
    Base Class for MkTables. Only deals with managing the data.
    graph TD
      1430734008["mktable.MkTable"]
      3678415322["mkbasetable.MkBaseTable"]
      835538629["mkcontainer.MkContainer"]
      1956333208["mkcontainer.MkContainerBase"]
      1359323199["mknode.MkNode"]
      2751680898["builtins.object"]
      3678415322 --> 1430734008
      835538629 --> 3678415322
      1956333208 --> 835538629
      1359323199 --> 1956333208
      2751680898 --> 1359323199
    /home/runner/work/mknodes/mknodes/src/mknodes/basenodes/mktable/metadata.toml
    [metadata]
    icon = "octicon:table-24"
    name = "MkTable"
    
    [requirements.extension.tables]
    
    [examples.regular]
    title = "Regular"
    jinja = """
    {{ {"Column A": ["A", "B", "C"], "Column B": ["C", "D", "E"]} | MkTable }}
    """
    
    [examples.rows]
    title = "From rows (the first one is the header unless `columns` are given)"
    jinja = """
    {{ [["Name", "Value"], ["a|b", "1"], ["c", "2"]] | MkTable }}
    """
    
    [examples.list_of_dicts]
    title = "From list of dicts"
    jinja = """
    {{ [{"col 1": "abc", "col 2": "cde"}, {"col 1": "fgh", "col 2": "ijk"}] | MkTable }}
    """
    
    # [output.markdown]
    # template = """
    # {% for k in table_data.keys() %}
    # {% endfor %}
    # {% for row in self.iter_rows() %}
    # {% endfor %}
    
    # """
    
    mknodes.basenodes.mktable.MkTable
    class MkTable(mkbasetable.MkBaseTable):
        """Class representing a formatted table.
    
        Data: a mapping column -> cells, a list of rows (the first one is the header unless
        `columns` are given) or a list of dicts (see `MkBaseTable`). `|` in cells gets escaped.
        """
    
        REQUIRED_EXTENSIONS = [resources.Extension("tables")]
    
        def to_md_unprocessed(self) -> str:
            table_data = self.data  # property
            if not any(table_data[k] for k in table_data):
                return ""
            in_code = rendering.current().name != "material"
            headers = [escape_cell(k, in_code_spans=in_code) for k in table_data]
            rows = [
                [escape_cell(str(cell), in_code_spans=in_code) for cell in row]
                for row in self.iter_rows()
            ]
            widths = [
                max(len(h), *(len(row[i]) for row in rows)) if rows else len(h)
                for i, h in enumerate(headers)
            ]
            formatters = [f"{{:<{width}}}" for width in widths]
            headers = [formatters[i].format(h) for i, h in enumerate(headers)]
            divider = [width * "-" for width in widths]
            data = [[formatters[i].format(c) for i, c in enumerate(row)] for row in rows]
            header_txt = "| " + " | ".join(headers) + " |"
            divider_text = "| " + " | ".join(divider) + " |"
            data_txt = ["| " + " | ".join(line) + " |" for line in data]
            return "\n".join([header_txt, divider_text, *data_txt]) + "\n"